package com.dhh.activity;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem.Type;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.manager.PlanManager;
import com.dhh.vo.FoodPlan;
import com.dhh.widget.CustomMenu.OnMenuItemSelectedListener;
import com.dhh.widget.CustomMenuItem;

public class FoodPlanActivity extends GDActivity implements OnMenuItemSelectedListener,View.OnClickListener {
	private DataBaseHelper db;
	private PlanManager pManager;
	private FoodPlan plan;
	
	private TextView qGrain;
	private TextView qVegetable;
	private TextView qFruit;
	private TextView qDairy;
	private TextView qProtein;
	private TextView dCalorie;
	private Button bDesignFood;
	
	private Button bGrain;
	private Button bVegetable;
	private Button bFruit;
	private Button bDairy;
	private Button bProtein;
	
	@Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setActionBarContentView(R.layout.food_plan);
        
        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();
        pManager = new PlanManager(db);         
        plan = pManager.getCurrentPlan();
        
        bDesignFood = (Button) this.findViewById(R.id.fp_designfood);
        bDesignFood.setOnClickListener(this);        
        qGrain = (TextView) this.findViewById(R.id.fp_grain_q);
        qVegetable = (TextView) this.findViewById(R.id.fp_vegetable_q);
        qFruit = (TextView) this.findViewById(R.id.fp_fruit_q);
        qDairy = (TextView) this.findViewById(R.id.fp_dairy_q);
        qProtein = (TextView) this.findViewById(R.id.fp_protein_q);
        dCalorie = (TextView) this.findViewById(R.id.fp_calorie);
        
        bGrain = (Button) this.findViewById(R.id.fp_grain_b);
        bVegetable = (Button) this.findViewById(R.id.fp_vegetable_b);
        bFruit = (Button) this.findViewById(R.id.fp_fruit_b);
        bDairy = (Button) this.findViewById(R.id.fp_dairy_b);
        bProtein = (Button) this.findViewById(R.id.fp_protein_b);
        
        bGrain.setOnClickListener(this);
        bVegetable.setOnClickListener(this);
        bFruit.setOnClickListener(this);
        bDairy.setOnClickListener(this);
        bProtein.setOnClickListener(this);
        
        getActionBar().setTitle("Daily Food Plan");
        getActionBar().setBackgroundColor(Color.rgb(14, 166, 249));
        getActionBar().getChildAt(0).
        	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(FoodPlanActivity.this,HomeActivity.class);	
					startActivity(intent);
				}}); 
        addActionBarItem(Type.Help).getItemView().
	    	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(FoodPlanActivity.this,ShowFGActivity.class);	
					startActivity(intent);
				}}); 
	}
	
	@Override
	public void onStart(){
		super.onStart();        
        fillData();
	}
	
	public void fillData(){
		qGrain.setText(plan.getGrain());
		qVegetable.setText(plan.getVegetable());
		qFruit.setText(plan.getFruit());
		qDairy.setText(plan.getDairy());
		qProtein.setText(plan.getProteinFood());	
		dCalorie.setText(plan.getEnergy());
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(FoodPlanActivity.this,SelectTopicActivity.class);
		if(v.getId() == R.id.fp_designfood){
			Intent myIntent = new Intent(FoodPlanActivity.this, DietLogActivity.class);
			this.startActivity(myIntent);
		}else if(v.getId() == R.id.fp_grain_b){
			intent.putExtra("food_group", "top_grain");
			this.startActivity(intent);
		}else if(v.getId() == R.id.fp_vegetable_b){
			intent.putExtra("food_group", "top_vegetable");
			this.startActivity(intent);
		}else if(v.getId() == R.id.fp_fruit_b){
			intent.putExtra("food_group", "top_fruit");
			this.startActivity(intent);
		}else if(v.getId() == R.id.fp_dairy_b){
			intent.putExtra("food_group", "top_dairy");
			this.startActivity(intent);
		}else if(v.getId() == R.id.fp_protein_b){
			intent.putExtra("food_group", "top_protein");
			this.startActivity(intent);
		}		
	}

	@Override
	public void MenuItemSelectedEvent(CustomMenuItem selection) {
	}

	@Override
    public void onResume(){
    	super.onResume();
        db.openDataBase();       
    }
    
    @Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
    
    @Override
	protected void onStop(){
		super.onStop();
	}
}
